﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Drawing;
using System.Xml.Linq;
using System.ComponentModel;


namespace CarInfoBusiness
{
    public class GetCarTypes
    {

        private static GetCarTypes gct;

        public static GetCarTypes Instance
        {
            get
            {
                if (gct == null)
                    gct = new GetCarTypes();
                return gct;
            }
        }

        private GetCarTypes()
        { }

        public List<CarInfoDBAccess.DBAccess_New.CSCarType> GetTypes(CarInfoDBAccess.DBAccess_New.CSBrand brand)
        {
            XElement TypeXml = Common.Instance.GetXml(brand.Url);
            if (TypeXml == null)
                return null;

            List<CarInfoDBAccess.DBAccess_New.CSCarType> list = new List<CarInfoDBAccess.DBAccess_New.CSCarType>();

            var data = from node in TypeXml.Descendants()
                       select node;
            foreach (var t in data)
            {
                if (t.Name.LocalName == "div")
                {
                    if (t.HasAttributes && t.Attributes("class").Count() == 1)
                        if (t.Attribute("class").Value == "db_ny_left_16")
                        {
                            CarInfoDBAccess.DBAccess_New.CSCarType ct = new CarInfoDBAccess.DBAccess_New.CSCarType();
                            ct.Name = t.Value;
                            var geturl = from u in t.Descendants()
                                         select u;
                            ct.Url = geturl.Single().Attribute("href").Value;
                            //ct. = brand.Id;

                            string id = ct.Url.Replace("http://db.chinacars.com/12/db_", "");
                            id = id.Replace(".htm", "");
                            ct.Id = Convert.ToInt32(id);

                            list.Add(ct);
                            //brand.CSCarType.Add(ct);
                        }

                }
            }
            return list;
        }


        public void GetTypes(List<CarInfoDBAccess.Model.Brand> list)
        {
            int count = 0;
            foreach (CarInfoDBAccess.Model.Brand b in list)
            {
                string result = " Done";
                if (!GetType(Common.Instance.GetXml(b.Url), b))
                    result = " failed";
                count++;
                Common.Instance.TextOutput("Get type " + count.ToString() + "/" + list.Count.ToString() + result);
                double process = (Convert.ToDouble(count) / Convert.ToDouble(list.Count)) * 100;
                Debug.WriteLine(process);
                Common.Instance.ChangeProcessBarValue(process);
            }
        }

        private bool GetType(XElement TypeXml, CarInfoDBAccess.Model.Brand b)
        {
            if (TypeXml == null)
                return false;
            if (b.CTList == null)
                b.CTList = new List<CarInfoDBAccess.Model.CarType>();

            var data = from node in TypeXml.Descendants()
                       select node;
            foreach (var t in data)
            {
                if (t.Name.LocalName == "div")
                {
                    if (t.HasAttributes && t.Attributes("class").Count() == 1)
                        if (t.Attribute("class").Value == "db_ny_left_16")
                        {
                            CarInfoDBAccess.Model.CarType ct = new CarInfoDBAccess.Model.CarType();
                            //ct.Id = ++LastCarTypeId;
                            ct.Name = t.Value;
                            var geturl = from u in t.Descendants()
                                         select u;
                            ct.Url = geturl.Single().Attribute("href").Value;
                            ct.BrandID = b.Id;

                            string id = ct.Url.Replace("http://db.chinacars.com/12/db_", "");
                            id = id.Replace(".htm", "");
                            ct.Id = Convert.ToInt32(id);

                            b.CTList.Add(ct);
                            //读取车详情
                           // BL.GetInfos.Instance.GetCar(ref ct);
                        }

                }
            }
            return true;
        }

    }
}
